package cn.sfa.common.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 隐藏敏感词汇
 * @author hui
 *
 */
public class WordFilter {
	private static Map<Character, List<String>> wordListToMap(
			List<String> sensitiveWordList) {
		Map<Character, List<String>> result = new HashMap<Character, List<String>>();
		for (String s : sensitiveWordList) {
			char c = s.charAt(0);
			List<String> strs = result.get(c);
			if (strs == null) {
				strs = new ArrayList<String>();
				result.put(c, strs);
			}
			strs.add(s);
		}

		return result;
	}

	public static String filter(String src, List<String> sensitiveWordList) {
		Map<Character, List<String>> wordMap = wordListToMap(sensitiveWordList);

		StringBuilder strb = new StringBuilder();
		for (int i = 0; i < src.length(); i++) {
			char c = src.charAt(i);
			String find = null;
			if (wordMap.containsKey(c)) {
				List<String> words = wordMap.get(c);
				for (String s : words) {
					String temp = src.substring(i,
							(s.length() <= (src.length() - i)) ? i + s.length()
									: i);
					if (s.equals(temp)) {
						find = s;
						break;
					}
				}
			}
			if (find != null) {
				strb.append("**");
				i += (find.length() - 1);
			} else {
				strb.append(c);
			}
		}
		return strb.toString();
	}

	/**
	 * @param args
	 */
	
	public String check(String s){
		List<String> wordList = new ArrayList<String>();
		WordFilter wordfilter = new WordFilter();
		wordList.add("TMD");
		wordList.add("变态");
		wordList.add("你妹的");
		wordList.add("操");
		wordList.add("蛋疼");
		wordList.add("人民内情真相");
		wordList.add("法轮");
		wordList.add("人民真实报道");
		wordList.add("人民之声论坛");
		wordList.add("人权");
		wordList.add("杀笔");
		wordList.add("傻逼");
	
	
	
		//wordlist.add("");
		return wordfilter.filter(s, wordList);
	}

}
